package com.huoyun.common.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;


import com.huoyun.base.dao.BaseDaoImpl;

import com.huoyun.cargo.model.ContactCarModel;
import com.huoyun.common.bean.BidInfo;
import com.huoyun.common.bean.BidInfoItem;
import com.huoyun.common.dao.evt.GetBidInfoByCarIdEvt;
import com.huoyun.common.dao.evt.GetBidInfoByGoodsIdEvt;
import com.huoyun.common.dao.evt.GetBidInfoItemEvt;
import com.huoyun.order.bean.Order;

public class BidInfoDaoImpl extends BaseDaoImpl<BidInfo> implements BidInfoDao{

	@Override
	public List<BidInfo> getEntryByCarId(long carId) {
		// TODO Auto-generated method stub
		return sqlSessionTemplate.selectList("com.huoyun.common.dao.BidInfoDaoMapper.getEntryByCarId",carId);
	}

	@Override
	public List<BidInfo> getEntryByGoodsId(long goodsId) {
		// TODO Auto-generated method stub
		return sqlSessionTemplate.selectList("com.huoyun.common.dao.BidInfoDaoMapper.getEntryByGoodsId",goodsId);
	}

	@Override
	public void saveCarDriver(BidInfo bidInfo) {
		// TODO Auto-generated method stub
		this.sqlSessionTemplate.insert("com.huoyun.common.dao.BidInfoDaoMapper.saveCargoDriver",bidInfo);
	}

	@Override
	public int getCountByCarId(long carId) {
		// TODO Auto-generated method stub
		List<Object> maps=sqlSessionTemplate.selectList("com.huoyun.common.dao.BidInfoDaoMapper.getEntryByCarId",carId);
		return maps.size() ;
	}

	@Override
	public int getCountByGoodsId(long goodsId) {
		// TODO Auto-generated method stub
		List<Object> maps=sqlSessionTemplate.selectList("com.huoyun.common.dao.BidInfoDaoMapper.getEntryByGoodsId",goodsId);
		return maps.size();
	}
	
	/**
	 * 根据车辆id查询应标数
	 * @param getBidInfoByCarIdEvt 查询参数
	 * @return 应标总数
	 */
	@Override
	public int getBidCountByCarId(GetBidInfoByCarIdEvt getBidInfoByCarIdEvt)
	{
		return this.sqlSessionTemplate.selectOne("com.huoyun.common.dao.BidInfoDaoMapper.getBidCountByCarId", getBidInfoByCarIdEvt);
	}
	
    /**
     * 根据车辆id查询应标列表
     * @param getBidInfoByCarIdEvt 查询参数
     * @return 应标列表
     */
	@Override
	public List<BidInfo> getBidInfoByCarId(GetBidInfoByCarIdEvt getBidInfoByCarIdEvt)
	{
		return this.sqlSessionTemplate.selectList("com.huoyun.common.dao.BidInfoDaoMapper.getBidInfoByCarId", getBidInfoByCarIdEvt);
	}
	
	/**
	 * 根据货物id查询应标数
	 * @param getBidInfoByGoodsIdEvt 查询参数
	 * @return 应标数
	 */
	@Override
	public int getBidCountByGoodsId(GetBidInfoByGoodsIdEvt getBidInfoByGoodsIdEvt)
	{
		return this.sqlSessionTemplate.selectOne("com.huoyun.common.dao.BidInfoDaoMapper.getBidInfoByGoodsId", getBidInfoByGoodsIdEvt);
	}
	
	/**
	 * 根据货物id查询应标列表
	 * @param getBidInfoByGoodsIdEvt 查询参数
	 * @return 应标列表
	 */
	@Override
	public List<BidInfo> getBidInfoByGoodsId(GetBidInfoByGoodsIdEvt getBidInfoByGoodsIdEvt)
	{
		return this.sqlSessionTemplate.selectList("com.huoyun.common.dao.BidInfoDaoMapper.getBidInfoByGoodsId", getBidInfoByGoodsIdEvt);
	}
	
	/**
	 * 更新应标信息的状态
	 * @param bidId 应标信息id
	 * @param status 要更新的状态
	 * 
	 * @author 刘晓宁
	 */
	@Override
	public void updateStatus(long bidId, int status)
	{
		Map<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("bidId", bidId);
		dataMap.put("status", status);
		
		this.sqlSessionTemplate.update("com.huoyun.common.dao.BidInfoDaoMapper.updateStatus", dataMap);
	}
	
	@Override
	public BidInfo getBidInfo(long goodsId, long carId)
	{
		Map<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("carId", carId);
		dataMap.put("goodsId", goodsId);
		
		//逻辑上限制只能查询一条，难免数据库有脏数据，为了保证不出错，取查询列表的第一个
		List<BidInfo> bidInfo = this.sqlSessionTemplate.selectList("com.huoyun.common.dao.BidInfoDaoMapper.getPubInfoByGoodsIdAndCarId", dataMap);
		
		if(bidInfo != null && bidInfo.size() > 0)
		{
			return bidInfo.get(0);
		}

		return null;
	}

	public List<ContactCarModel> getContactCarModelsByGoodsId(long goodsId) {
		return this.sqlSessionTemplate.selectList("com.huoyun.common.dao.BidInfoDaoMapper.getContactCarModelsByGoodsId", goodsId);
	}
	
	public List<BidInfo> getBidInfoListByUserId(long userId) {
		return this.sqlSessionTemplate.selectList("com.huoyun.common.dao.BidInfoDaoMapper.getBidInfoListByUserId", userId);
	}
	@Override
	public BidInfo getEntryById(long bidId) {
		// TODO Auto-generated method stub
		return sqlSessionTemplate.selectOne("com.huoyun.common.dao.BidInfoDaoMapper.getEntryById",bidId);
	}

	@Override
	public Order GenerateOrderByBidId(long bidId) {
		// TODO Auto-generated method stub
		return sqlSessionTemplate.selectOne("com.huoyun.common.dao.BidInfoDaoMapper.GenerateOrderByBidId",bidId);
	}

	@Override
	public void updateSuccess(long bidId,int success) {
		// TODO Auto-generated method stub
		Map map=new HashMap();
		map.put("bidId", bidId);
		map.put("success", success);
		sqlSessionTemplate.update("com.huoyun.common.dao.BidInfoDaoMapper.updateSuccess",map);
	}

	@Override
	public List<ContactCarModel> getSelectedCar(long goodsId, int isSuccess) {
		// TODO Auto-generated method stub
		Map map=new HashMap();
		map.put("goodsId", goodsId);
		map.put("isSuccess", isSuccess);
		return sqlSessionTemplate.selectList("com.huoyun.common.dao.BidInfoDaoMapper.getSelectedCar",map);
	}



	@Override
	public List<Order> GenerateOrdersByGoodsId(long goodsId) {
		// TODO Auto-generated method stub
		return sqlSessionTemplate.selectList("com.huoyun.common.dao.BidInfoDaoMapper.GenerateOrdersByGoodsId",goodsId);
	}
	
	@Override
	public long getBidInfoItemByStatusForUserCount(
			GetBidInfoItemEvt getBidInfoItemEvt) {
		// TODO Auto-generated method stub
		return sqlSessionTemplate.selectOne("com.huoyun.common.dao.BidInfoDaoMapper.getBidInfoItemByStatusForUserCount",getBidInfoItemEvt);
	}

	@Override
	public List<BidInfoItem> queryBidInfoItemByStatusForUser(GetBidInfoItemEvt getBidInfoItemEvt) {
		// TODO Auto-generated method stub
		
		return sqlSessionTemplate.selectList("com.huoyun.common.dao.BidInfoDaoMapper.queryBidInfoItemByStatusForUser",getBidInfoItemEvt);		
	}
	@Override
	public List<BidInfoItem> getBidInfoItemByStatusForUser(long userId,int status){
		Map<Object,Object> map=new HashMap<Object,Object>();
		map.put("userId", userId);
		map.put("status", status);
		return sqlSessionTemplate.selectList("com.huoyun.common.dao.BidInfoDaoMapper.getBidInfoItemByStatusForUser",map);	
	}
	
	@Override
	public void deleteBidInfoById(long bidInfoId) {
		sqlSessionTemplate.delete("com.huoyun.common.dao.BidInfoDaoMapper.deleteBidInfoById", bidInfoId);
	}

	@Override
	public void updateBidInfoByBidInfo(BidInfo bidInfo) {
		// TODO Auto-generated method stub
		sqlSessionTemplate.update("com.huoyun.common.dao.BidInfoDaoMapper.updateBidInfoByBidInfo", bidInfo);
	}

	@Override
	public ContactCarModel getContactCarByBidId(long bidId) {
		// TODO Auto-generated method stub
		return sqlSessionTemplate.selectOne("com.huoyun.common.dao.BidInfoDaoMapper.getContactCarByBidId",bidId);
	}

	@Override
	public List<ContactCarModel> getContactCarsByGoodsId(long goodsId) {
		// TODO Auto-generated method stub
		return sqlSessionTemplate.selectList("com.huoyun.common.dao.BidInfoDaoMapper.getContactCarsByGoodsId",goodsId);
	}


}
